# -*- coding: utf-8 -*-
"""
Created on Mon Aug 13 10:57:24 2018

@author: jiujiujiu
"""

import requests
import re
from cs import day
from WriteData import writedata
from Tk import genearteMD5


# 获取单个页面
def getHTMLText(url):
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}
    try:

        r = requests.get(url, timeout=30, headers=header)
        r.raise_for_status()  # 如果状态不是200, 引发HTTPError异常

        r.encoding = r.apparent_encoding
        return r.text

    except:
        return None


def get_data(url):
    html = getHTMLText(url)
    if not html:
        return None
    rr = re.compile(r'<div class="post-date-ribbon"><div class="corner"></div>([\s\S]*?)</div>')
    datas = re.findall(rr, html)
    return datas


def count_process_one():  # 资讯
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    while 1:
        s = d1 + d2 + d3
        url = 'http://www.lianshangcaijingcn.com/news.asp?a=50&b=&p=16&page={}'
        url = url.format(a)
        datas = get_data(url)
        if not datas:
            break
        print('---- 链尚财经资讯获取第{}页----'.format(a))
        for date in datas:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        a += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def main():
    u = '链尚财经'

    t1 = count_process_one()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0], "yesterday": t1[1], "frontday": t1[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
